/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.installer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

public class SSHTunnelerInstallerApp {

	protected void start() throws URISyntaxException {

		ShowLicense accept_license = new ShowLicense(Config.inst.getLicense());
		accept_license.setVisible(true);
		if (!accept_license.isAccepted()) {
			JOptionPane.showMessageDialog(null,
					"License was not accepted, quitting.");
			System.exit(-1);
		}

		if (Config.inst.ask_username) {
			String username = JOptionPane.showInputDialog(null,
					"What is your username?");

			if (username == null || username.equals("")) {
				JOptionPane.showMessageDialog(null,
						"Username is mandatory for installation.");
				System.exit(-1);
			}
			Config.inst.username = username;
		}

		try {
			// Launcher
			if (!Config.inst.getLauncherGenerator().generate_launcher()) {
				if (JOptionPane
						.showConfirmDialog(
								null,
								"Could not create launcher file, continue installation?",
								"Error creating launcher",
								JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
					System.exit(-1);
				}
			}

			// Config
			File configFile = Config.inst.getConfigFile();
			boolean write_config = true;
			if (configFile.exists() && !Config.inst.overwrite_all) {
				int ret = JOptionPane.showConfirmDialog(null, "Config file ("
						+ configFile.getAbsolutePath()
						+ ") already exist. Replace ? ");
				if (ret != JOptionPane.YES_OPTION) {
					write_config = false;
				}
			}
			if (write_config) {
				FileOutputStream stream = new FileOutputStream(configFile);
				PrintStream out = new PrintStream(stream);
				out.print(Config.inst.getDefaultConfig());
				out.close();
				stream.close();
			}

			File jarFile = new File(SSHTunnelerInstallerApp.class
					.getProtectionDomain().getCodeSource().getLocation()
					.toURI());
			String jar_name = jarFile.getName();
			if (new Installer(jar_name).exec_script()) {
				JOptionPane.showMessageDialog(null, "Installation completed.");
			}
		} catch (FileNotFoundException ex) {
			String message = ex.getMessage();
			if (Config.inst.create_debug_link) {
				message = message + "\n" + ex.getStackTrace();
			}
			JOptionPane.showMessageDialog(null, "Error while installing: \n"
					+ message);
		} catch (IOException ex) {
			String message = ex.getMessage();
			if (Config.inst.create_debug_link) {
				message = message + "\n" + ex.getStackTrace();
			}
			JOptionPane.showMessageDialog(null, "Error while installing: \n"
					+ message);
		}
	}

	public static void main(String[] args) {
		try {
			new SSHTunnelerInstallerApp().start();
		} catch (URISyntaxException ex) {
			Logger.getLogger(SSHTunnelerInstallerApp.class.getName()).log(
					Level.SEVERE, null, ex);
		}
	}
}
